package com.wny.activities;

import java.io.IOException;
import java.net.UnknownHostException;

import com.wny.R;
import com.wny.backend.SocketsClient;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class CreateEventActivity extends Activity implements LocationListener {

	Button subEventBtn;
	OnClickListener subEventListener = null;
	SocketsClient socket;
	private Location myLocation;
	LocationManager myProvider;
	int uid;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		myProvider = (LocationManager) getSystemService(LOCATION_SERVICE);
		setContentView(R.layout.create_event);
		subEventListener = new OnClickListener() {
			public void onClick(View v) {
				trySubmitEvent(v);
			}
		};
		Bundle extras = getIntent().getExtras();
		uid = extras.getInt("uid");
		subEventBtn = (Button) findViewById(R.id.submitevent);
		subEventBtn.setOnClickListener(subEventListener);
	}

	public void trySubmitEvent(View V) {
		String eventName;
		String eventLocation;
		String description;
		StringBuffer toSend;
		int day;
		int month;
		int year;
		int Hour;
		int Minute;
		socket = SocketsClient.getSocket();
		// pulls the event name, location, and description and sends it to the
		// server
		eventName = ((EditText) findViewById(R.id.eventname)).getText()
				.toString();
		eventLocation = ((EditText) findViewById(R.id.eventlocation)).getText()
				.toString();
		description = ((EditText) findViewById(R.id.editText3)).getText()
				.toString();

		// uses date picker class to get event information
		DatePicker dp = (DatePicker) findViewById(R.id.datePicker);
		day = dp.getDayOfMonth();
		month = dp.getMonth() + 1;
		year = dp.getYear();

		// time picker used to get hour and minute
		TimePicker tp = (TimePicker) findViewById(R.id.timePicker);

		Hour = tp.getCurrentHour();
		Minute = tp.getCurrentMinute();

		toSend = new StringBuffer("add=event&title=" + eventName
				+ "&latitude=0&longitude=0&date=" + year + "-" + month + "-"
				+ day + " "+ Hour+":"+Minute+":0"+"&description=" + description + "\n");

		// checks to see if information sent to the server
		try {
			char[] result = socket.sendData(toSend.toString().getBytes());
			if (result[0] == '0')
				displayWithToast("Problem making event):", 4000);
			else if (result[0] == '1') {
				displayWithToast("Sucessfuly added event (:", 4000);
				// setContentView(R.layout.loggedinview);
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// a wrapper function for the Toast class. Used to show messages onscreen
	private void displayWithToast(String toShow, int duration) {
		Toast temp = Toast.makeText(this, toShow, duration);
		temp.setGravity(Gravity.CENTER, 0, 0);
		temp.show();
	}
	public void onLocationChanged(Location location)
	{
		// TODO Auto-generated method stub
		SocketsClient myComm = SocketsClient.getSocket();
		this.myLocation = location;
		try {
			myComm.sendNonblocking(("loc&"+location.getLatitude()+"&"+location.getLongitude()+"&"+this.uid+"\n").getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	//notifies user GPS disables
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		displayWithToast("The system GPS service has been disabled.", 5000);
	}

	//notifies user GPS enabled
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		displayWithToast("GPS is now avaliable", 4000);

	}

	//notifies user of any change in the status of the GPS
	public void onStatusChanged(String location_provider, int status, Bundle otherVariables) {
		// TODO Auto-generated method stub
		switch (status)
		{
		case LocationProvider.OUT_OF_SERVICE:
			displayWithToast("The GPS is out of service.", 5000);
		case LocationProvider.TEMPORARILY_UNAVAILABLE:
			displayWithToast("The GPS is temporarially unavaliable", 5000);
		case LocationProvider.AVAILABLE:
			displayWithToast("GPS is now avaliable", 5000);
		}

	}
}
